﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace IdragonetEye
{
    public partial class Form_Eye_Main : Form
    {
        private bool _bool_isRun = false;
        string[] args = null;

        public Form_Eye_Main(string[] args)
        {
            InitializeComponent();
            this.args = args;
        }

        private void Form_Eye_Main_Load(object sender, EventArgs e)
        {
          checkBox_winstart.Checked = IdragonetEye.Class_winstart.Isturn();
          checkBox_winstart.CheckedChanged += checkBox_winstart_CheckedChanged;

            numericUpDown_xx.Value = Convert.ToInt16(ConfigurationManager.AppSettings["xx"]);
            numericUpDown_js.Value = Convert.ToInt16(ConfigurationManager.AppSettings["js"]);

            label_tooltips.Text = "";
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            if (!_bool_isRun) //开始运行
            {
                timer_xx.Interval = Convert.ToInt16(numericUpDown_js.Value)*60*1000;
                timer_xx.Enabled = true;
                _bool_isRun = true;
                button_start.Text = " 结 束";

                numericUpDown_xx.Enabled = false;
                numericUpDown_js.Enabled = false;
                notifyIcon_Eye.Text = "小龙护眼精灵 -- 计时中";

                this.WindowState = FormWindowState.Minimized;

            }
            else
            {
                timer_xx.Enabled = false;
                _bool_isRun = false;
                button_start.Text = " 开 始";
                notifyIcon_Eye.Text = "小龙护眼精灵";

                numericUpDown_xx.Enabled = true;
                numericUpDown_js.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var form = new Form_force(checkBox_force.Checked, (int)numericUpDown_xx.Value*60);
            form.ShowDialog();
        }

        private void Form_Eye_Main_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                notifyIcon_Eye.Visible = true;
            } 
        }

        private void notifyIcon_Eye_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            notifyIcon_Eye.Visible = false; 
        }

        private void timer_xx_Tick(object sender, EventArgs e)
        {
            label_tooltips.Text =  " 上次休息: "+DateTime.Now ;
            timer_xx.Enabled = false;

            var form = new Form_force(checkBox_force.Checked, (int)numericUpDown_xx.Value * 60);
            var t1 = form.ShowDialog();

            timer_xx.Enabled = true;
        }



        private void checkBox_winstart_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_winstart.Checked)
            {
                IdragonetEye.Class_winstart.turnOn();
            }
            else
            {
                IdragonetEye.Class_winstart.turnOff(); 
            }
        }

        private void numericUpDown_js_ValueChanged(object sender, EventArgs e)
        {
            Class_appset.SetKey("js", Convert.ToString(numericUpDown_js.Value));
        }

        private void numericUpDown_xx_ValueChanged(object sender, EventArgs e)
        {
            Class_appset.SetKey("xx", Convert.ToString(numericUpDown_xx.Value));
        }

        private void Form_Eye_Main_Shown(object sender, EventArgs e)
        {
            if (args.Count()!=0 && args[0] == "-s")
            {
                button_start.PerformClick();
                this.WindowState = FormWindowState.Minimized;
            }

            this.Shown -= Form_Eye_Main_Shown;
        }
    }
}
